<?php
	####################################################
	# library import and object instance.              #
	####################################################
	# 커먼 인클루드
	include_once( '../common.php' );

	####################################################
	# request data($_POST AND $_GET FROM $_REQUEST)    #
	####################################################
	# REQUEST(get and post variables)
	$req = Request::clean_request($_REQUEST, FALSE);

	# GET(url variables)

	# POST(form variables)

	# Validation(special case validate)

	####################################################
	# implementation                                   #
	####################################################
	$sec->individual_access();

	$pagenation_cfg1 = array(
		'uri'				=> $PHP_SELF,
		'articles_count'	=> 30,
		'req_page'			=> empty($req['p1'])? 0 : $req['p1'],
		'page_size'			=> 6,
		'articles_cnt'		=> $db->getcnts('ggo_cfg_categories_pref'),
		'style'				=> Pagenation::$style_white,
		'get'				=> 'p1'
	);
	$categories = $appm->get_categories($pagenation_cfg1['req_page'] * $pagenation_cfg1['articles_count'], $pagenation_cfg1['articles_count']);
	$pagenation1 = Pagenation::out($pagenation_cfg1);
	//Nlog::puts('categories',$categories);

	$modules = $appm->get_modules(0, $db->getcnts('ggo_cfg_modules_pref'));
	//Nlog::xmp($modules);

	#intersection $categories['res'] of $val['modules']
	if($categories['has']) foreach($categories['res'] as $key=>$val){
		$a = array();
		$b = array();
		foreach($modules['res'] as $k=>$v){
			$a[] = $v['muid'];
		}
		foreach($val['modules'] as $k=>$v){
			$b[] = $v['muid'];
		}
		$choose_able[$val['cuid']] = array_intersect($a, $b);
	}

	//Nlog::xmp($choose_able);
?>



<?php
	# HTML START
	echo $html->custom_header(TITLE);
	echo $tplt->page_construct_javascript();
	echo $html->custom_js("{$JS}common.js");
	echo $html->custom_css("{$CSS}common.css");
	echo $html->custom_body();
?>
	<?php $tplt->app_head($NAVI)?>
	<?php $tplt->tiles_admin_sidebar($NAVI)?>
	<?php $tplt->tiles_admin_contents($pkname, $pname, $pdesc)?>

		<h4>카테고리</h4>
		<?php echo NForm::open(NULL, FALSE, "list");?>
			<?php echo NForm::mode('modify')?>
			<table class="tbl" border="0" cellpadding="3" cellspacing="0">
				<colgroup>
					<col width="40" />
					<col width="40" />
					<col width="*" />
					<col width="*" />
					<col width="*" />
					<col width="60" />
				</colgroup>
				<tr>
					<th><input type="checkbox" id="chkctr1" onclick="CheckboxControl.toggle_check_all('#chkctr1','.chk1');"/></th>
					<th>번호</th>
					<th>카테고리 이름</th>
					<th>시작할 모듈 명</th>
					<th>연결된 모듈들</th>
					<th>모듈<br />연결</th>
				</tr>
				<?php if($categories['has']) foreach($categories['res'] as $key=>$val):?>
					<tr>
						<td>
							<input type="checkbox" name="map[<?=$val['cuid']?>][cuid]" id="chk1_<?=$val['cuid']?>" class="chk1" value="<?=$val['cuid']?>" />
						</td>
						<td><?=$val['cuid']?></td>
						<td>
							<input type="text" name="map[<?=$val['cuid']?>][category_name]" value="<?=$val['category_name']?>" maxlength="100" onfocus="CheckboxUi.focus_to_check('#chk1_<?=$val['cuid']?>');" class="inp wmatch" />
						</td>
						<td>
							<select name="map[<?=$val['cuid']?>][category_init_modules]" class="wmatch" onfocus="CheckboxUi.focus_to_check('#chk1_<?=$val['cuid']?>');" >
								<?php if($modules['has']) foreach($modules['res'] as $k=>$v):?>
									<?php if(in_array($v['muid'], $choose_able[$val['cuid']])):?>
										<option value="<?=$v['muid']?>" <?=NForm::selected($v['muid'], $val['category_init_modules'])?>>
											<?=$v['modules_name']?> (<?=$v['muid']?>)
										</option>
									<?php endif;?>
								<?php endforeach;?>
							</select>
						</td>
						<td>
							<table class="tbl" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<th>번호</th>
									<th>모듈 이름</th>
								</tr>
							<?php foreach($val['modules'] as $key=>$val) :?>
							<tr>
								<td><?=$val['muid']?></td>
								<td><?=$val['modules_name']?></td>
							</tr>
							<?php endforeach;?>
							</table>
						</td>
						<td>
							<span class="cbtn_s" onclick="Mover.href('admin.categories.linkage?cuid=<?=$val['cuid']?>')">연결</span>
						</td>
					</tr>
				<?php endforeach;?>
			</table>
		<?php echo NForm::close();?>
		<div class="tblt">
			<?=$pagenation1?>
		</div>
		<div class="tblh">
			체크된 카테고리를 : <span class="cbtn_s" onclick="$('#formx_list').submit();">수정</span>&nbsp;
			체크된 카테고리를 : <span class="cbtn_s" onclick="$('#formx_list [name=mode]').val('delete'); $('#formx_list').submit();">삭제</span>&nbsp;
		</div>
		<br />





		<h4>카테고리 추가</h4>
		<?php echo NForm::open(NULL, FALSE, "create");?>
			<?php echo NForm::mode('create')?>
			<table class="tbl" border="0" cellpadding="0" cellspacing="0">
				<colgroup>
					<col width="*" />
					<col width="*" />
					<col width="80" />
				</colgroup>
				<tr>
					<th>카테고리 이름</th>
					<th>시작할 모듈 명</th>
					<th>명령</th>
				</tr>
				<tr>
					<td>
						<input type="text" name="map[category_name]" maxlength="100" value="" class="inp wmatch" />
					</td>
					<td>
						<select name="map[category_init_modules]" class="wmatch">
							<?php if($modules['has']) foreach($modules['res'] as $key=>$val):?>
								<option value="<?=$val['muid']?>">
									<?=$val['modules_name']?> (<?=$val['muid']?>)
								</option>
							<?php endforeach;?>
						</select>
					</td>
					<td>
						<span class="cbtn_s" onclick="$('#formx_create').submit();">등록</span>
					</td>
			</table>
		<?php echo NForm::close();?>


<?php
	# HTML END
	echo $tplt->tiles_admin_footer();
	echo $tplt->page_destruct_javascript();
	include_once( '../endproc.php' );
	echo $html->html_footer();
?>